<%--@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1" --%>

<%@page import="uk.ac.sanger.DataManager" import ="java.util.*" %>
<%@ page import="uk.ac.sanger.*" import="uk.ac.sanger.DataManagerInterface" %>
<%@page import="com.sun.org.apache.bcel.internal.generic.IFNULL"%>
<%@page import="uk.ac.sanger.servlet.FeatureUpload"%>
<jsp:include page="header.jsp"/>


<TITLE>DAS writeback servlet</TITLE>
<%--<meta name="collection" content="exclude">--%>

<%   
    FeatureUpload ftrUp = new FeatureUpload();
  
	// status and list of missing values 
	String status = request.getParameter("arg");
	String missing = request.getParameter("missing");
	
	//coordinate system
	String system_uri = request.getParameter("uri");
   
	DataManager dbM = new DataManager();
	String system=""+dbM.getIdFromUri(system_uri);
	String system_name = dbM.new_system_name;
	session.setAttribute("system_name",system_name);
	session.setAttribute("system",system);
	session.setAttribute("system_uri",system_uri);
	   
		////////////////////////////////////////////////		
		//	check user log-in
		if((String)session.getAttribute("openid")==null){
			String message = "you are not logged in, please log in first!";
			session.setAttribute("message", message);
			response.sendRedirect("login.jsp");
			return;
		}
	    /////////////////////////////////////////////////
	        // means that feature is not new
			if (session != null & missing==null) {		
	
				FeatureBean features = dbM.getFeatureById(request.getParameter("id"), system_uri) ;
          
				if (features!=null){	
					session.setAttribute("featureid", features.getFeatureid());
					session.setAttribute("featuretype", features.getFeaturetype());
					session.setAttribute("method", features.getMethod());
					session.setAttribute("segmentid", features.getSegment());
					session.setAttribute("start", features.getStart());
					session.setAttribute("end", features.getEnd());
					session.setAttribute("strand", features.getStrand());
					session.setAttribute("phase", features.getPhase());
					session.setAttribute("score", features.getScore());

					//extract note, link from attribute
					String note="",links="",group="";
					if (features.getNotes().toString()!=""){
						if(features.getNotes().contains("link=")){;
							if(features.getNotes().contains("note=")){	
								links=features.getNotes().substring(5,(features.getNotes().indexOf("note=")-1));						
							}
							else {
								if(features.getNotes().contains("group=")){
									links=features.getNotes().substring(5,(features.getNotes().indexOf("group=")-1));						
								}
								else{
									links=features.getNotes().substring(5,features.getNotes().length()-1);			
								}
							}
					}
					if(features.getNotes().contains("note=")){
						if(features.getNotes().contains("group=")){
							note=features.getNotes().substring((features.getNotes().indexOf("note=")+5),(features.getNotes().indexOf("group=")-1));						
						}
						else{
							note=features.getNotes().substring((features.getNotes().indexOf("note=")+5), features.getNotes().length());
						}
					}
					if(features.getNotes().contains("group=")){
						group=features.getNotes().substring((features.getNotes().indexOf("group=")),features.getNotes().length());						
					}
					}

					session.setAttribute("attributes",note);
					session.setAttribute("link",links);
					session.setAttribute("group",group);		
				}				
				else{
					session.setAttribute("featureid", "");
					session.setAttribute("featuretype", "");
					session.setAttribute("method", "");
					session.setAttribute("segmentid", "");
					session.setAttribute("start", "");
					session.setAttribute("end", "");
					session.setAttribute("strand", "");
					session.setAttribute("phase", "");
					session.setAttribute("score", "");
					session.setAttribute("attributes","");
					session.setAttribute("link","");
					session.setAttribute("group","");	
				}
			}
		
		
		
		////////////////////////////////////////////////
		
	//these inserted values are stored in session variables
	String featureid = "", featuretype = "", method = "";
	// REMOVE  segmentid ="ENSG00000139618"  AFTER TEST!!!
	String segmentid = ""; //"ENSG00000139618";
	String start="";
	if (request.getParameter("start")!=null)
		start =  request.getParameter("start");
	String end ="";
	if ( request.getParameter("end")!=null)
		end = request.getParameter("end"); 
	String strand = "", phase = "", score = "";
	String attributes = "", link = "http://";
	
	//preselected values
	String mode = ""; //1=add, 2=modify, 3=delete
	String mode_mody="",mode_del="";
	String strand1 = "", strand2 = "", strand3 = "";
	String phase1 = "", phase2 = "", commit_msg=""; 
	String groups="", selected_group="", group_link="", group_del_link="";
	
	// new feature, show error message, don't remove inserted values
	if (status != null & missing != null) {
	    
		if (status.equals(ftrUp.CREATE_CMD)) {			
			status = "<p><FONT COLOR=red> Sorry! It looks like something is wrong or missing. Please go back and fill in the following items so we can continue: </FONT></p>";
			missing = missing.replace("@", "<BR>");
			missing = "<FONT COLOR=red>" + missing + " </FONT>";
		}
		if (status.equals(ftrUp.MODIFY_CMD)) {
			status = "Thank you for submitting your data! The annotation has been saved into the database.";
			missing = "";
		}
	    
		if (status.equals("NA")) {
			status = "";
			missing = "";
		}
		
		featureid = (String) session.getAttribute("featureid");	
		featuretype = (String) session.getAttribute("featuretype");
		method = (String) session.getAttribute("method");
		segmentid = (String)session.getAttribute("segmentid");
		start = (String) session.getAttribute("start");
		end = (String) session.getAttribute("end");
		strand = (String) session.getAttribute("strand");
		if (strand!=null){
			if (strand.equals("+")) {
				strand1 = "selected=\"selected\"";
				strand2 = "";
				strand3 = "";
			} else if (strand.equals("-")) {
				strand2 = "selected=\"selected\"";
				strand1 = "";
				strand3 = "";
			} else {
				strand3 = "selected=\"selected\"";
				strand1 = "";
				strand2 = "";
			}
		}
		phase = (String) session.getAttribute("phase");
		if(phase!=null){
			if (phase.equals(".")) 
				phase1 = "selected=\"selected\"";
			else
				phase2 = "selected=\"selected\"";
		}
		score = (String) session.getAttribute("score");
		attributes = (String) session.getAttribute("attributes");
		link = (String) session.getAttribute("link");
	} else {
		status = "";
		missing = "";
	}
    //disable/enable fields in different modes: 
    //1=add(all fields available),
    //2=modify(featureid blocked),
    //3=delete(all fields blocked)
	mode = (String) session.getAttribute("mode");
	  
	//	get data from groups table
	List get_groups = dbM.getGroups(system_uri);

	String list_groups="";
	if(get_groups!=null){
		for (int i=0;i<get_groups.size();i++) {
			if (list_groups.length()==0)
				list_groups=get_groups.get(i).toString();
			else
				list_groups=list_groups+";"+get_groups.get(i).toString();					
		}
	}
	session.setAttribute("groups", list_groups);
	//list of all groups
	groups=(String) session.getAttribute("groups");
	//saved group : group=...
    selected_group= (String) session.getAttribute("group");

	//handle group drop-down list
    if (selected_group!=null ) {
    	if( !selected_group.equals("")){
    
       		 selected_group=selected_group.substring(6,selected_group.length());
    		//list of groups contains such group
    		if(groups.indexOf(selected_group) > 0){
    			while(groups.indexOf(";")>0){
    			    if (groups.indexOf(";")==(groups.indexOf(selected_group)-1))
    					groups=groups.replaceFirst(";","<OPTION selected=\"selected\">");
    			    else
    			    	groups=groups.replaceFirst(";","<OPTION>");
    		}
    	}}
    	groups=groups.replaceAll(";","<OPTION>");
    }
    else
		groups=groups.replaceAll(";","<OPTION>");
	
	// enable-disable fields
	if (mode != null) {
		if (mode.equals(ftrUp.CREATE_CMD)) {
			mode_mody = "background-color:white;";
			mode_del="background-color:white;";
			group_link="add";
			group_del_link="delete";
			} 
		else if (mode.equals(ftrUp.MODIFY_CMD)) {
			mode_mody = "background-color:#CCCCCC; \" readonly" ;
			mode_del="background-color:white;";
			featureid = (String) session.getAttribute("featureid");
			featuretype = (String) session.getAttribute("featuretype");
			method = (String) session.getAttribute("method");
			segmentid = (String)session.getAttribute("segmentid");
			start = (String) session.getAttribute("start");
			end = (String) session.getAttribute("end");
			strand = (String) session.getAttribute("strand");
			if (strand!=null){
				if (strand.equals("+")) {
					strand1 = "selected=\"selected\"";
					strand2 = "";
					strand3 = "";
				} else if (strand.equals("-")) {
					strand2 = "selected=\"selected\"";
					strand1 = "";
					strand3 = "";
				} else {
					strand3 = "selected=\"selected\"";
					strand1 = "";
					strand2 = "";
				}
			}
			phase = (String)session.getAttribute("phase");
			if(phase!=null){
				if (phase.equals(".")) 
					phase1 = "selected=\"selected\"";
				else
					phase2 = "selected=\"selected\"";
			}
			score = (String) session.getAttribute("score");
			attributes = (String) session.getAttribute("attributes");
			link = (String) session.getAttribute("link");
			group_link="add";
			group_del_link="delete";
		}
		else if(mode.equals(ftrUp.DELETE_CMD)){
			mode_mody="background-color:#CCCCCC; \"readonly\"" ;
			mode_del ="background-color:#CCCCCC; \"readonly\"" ;
			featureid = (String) session.getAttribute("featureid");
			featuretype = (String) session.getAttribute("featuretype");
			method = (String) session.getAttribute("method");
			segmentid = (String)session.getAttribute("segmentid");
			start = (String) session.getAttribute("start");
			end = (String) session.getAttribute("end");
			strand = (String) session.getAttribute("strand");
			if (strand!=null){
				if (strand.equals("+")) {
					strand1 = "selected=\"selected\"";
					strand2 = "";
					strand3 = "";
				} else if (strand.equals("-")) {
					strand2 = "selected=\"selected\"";
					strand1 = "";
					strand3 = "";
				} else {
					strand3 = "selected=\"selected\"";
					strand1 = "";
					strand2 = "";
				}
			}
			phase = (String)session.getAttribute("phase");
			if(phase!=null){
				if (phase.equals(".")) 
					phase1 = "selected=\"selected\"";
				else
					phase2 = "selected=\"selected\"";
			}
			score = (String) session.getAttribute("score");
			attributes = (String) session.getAttribute("attributes");
			link = (String) session.getAttribute("link");
			group_link="";
			group_del_link="";
		}
	}
%>

<%-- <div align="center">  --%>


<!--  http://www.ensembl.org/img/nav-bg.gif-->
<FORM NAME="form" METHOD="POST" ACTION="/writebackServlet/writebackServlet">

<!-- <FORM METHOD="POST" ACTION="/ExampleDynamicWeb/post_writeback"> -->

<h1 align="center"><%=status%></h1>
<h1 align="center"><%=missing%></h1>

<div id="table">

<table>

<tr id="row0">
  <td colspan="4"><H2>Please insert your data <BR> (<%=system_name%>)</H2></td>
</tr>
<tr id="row1">
  <td  colspan="3" scope="colgroup"><P>Please be sure to fill in all items marked with a red <font color="#FF3333"><B>*</B></font></P> </td>
</tr>


<TR id="row1">
<TD ALIGN="RIGHT">FEATURE ID<FONT COLOR="#FF0033"><B>*</B></FONT></TD>
<TD> <INPUT TYPE="text" NAME="featureid" VALUE="<%= featureid %>" SIZE=50 MAXLENGTH=30 style="width:350px;height:18px; <%= mode_mody%> "></TD>
</TR>

<TR id="row2">
<TD ALIGN="RIGHT">FEATURE TYPE<FONT COLOR="#FF0033"><B>*</B></FONT></TD>
<TD><INPUT TYPE="text" NAME="featuretype" VALUE="<%= featuretype %>" SIZE=50 MAXLENGTH=50 style="width:350px;height:18px; <%= mode_del%> "></TD>
</TR>

<TR id="row1">
<TD ALIGN="RIGHT">METHOD<FONT COLOR="#FF0033"><B>*</B></FONT></TD>
<TD><INPUT TYPE="text" NAME="method" VALUE="<%= method %>" SIZE=50 MAXLENGTH=50 style="width:350px;height:18px; <%= mode_del %>"></TD>
</TR>

<TR id="row2">
<TD ALIGN="RIGHT"> SEGMENT ID <FONT COLOR="#FF0033"><B>*</B></FONT></TD>
<TD><INPUT TYPE="text" NAME="segmentid" VALUE="<%= segmentid %>" SIZE=50 MAXLENGTH=30 style="width:350px;height:18px; <%= mode_del %>" ></TD>
</TR>

<TR id="row1">
<TD ALIGN="RIGHT">START<FONT COLOR="#FF0033"><B>*</B></FONT></TD>
<TD><INPUT TYPE="text" NAME="start" VALUE="<%= start %>" 
ALIGN="RIGHT" SIZE=10 MAXLENGTH=10 style="width:80px;height:18px; <%= mode_del %>;"> 
E N D   <FONT COLOR="#FF0033"><B> * </B></FONT>
<INPUT TYPE="text" NAME="end" VALUE="<%= end %>" ALIGN="RIGHT" SIZE=8 MAXLENGTH=10 style="width:75px;height:18px; <%= mode_del %>;">
<INPUT TYPE="CHECKBOX"  NAME="no_pos" VALUE="235" style="font-family:verdana;font-size:80%;color:green" 
<% if (mode!=null) { if (mode.equals(ftrUp.DELETE_CMD)) {%> disabled  <% }} %>
<% if (mode!=null) { if (!mode.equals(ftrUp.DELETE_CMD)) {%> enabled  <% }} %>
>No position<BR>
</TD>
</TR>

<TR id="row2">
<TD ALIGN="RIGHT">STRAND<FONT COLOR="#FF0033"><B>*</B></FONT></TD>
<TD><select name="strand" 
<% if (mode!=null) { if (mode.equals(ftrUp.DELETE_CMD)) {%> style="width:80px; <%= mode_del%>" disabled <% }} %>
<% if (mode!=null) { if (!mode.equals(ftrUp.DELETE_CMD)) {%> style="width:80px" enabled <% }} %>
> <OPTION <%= strand1 %>> + <OPTION <%= strand2 %>> - <OPTION <%= strand3 %>> 0
</select>

PHASE<FONT COLOR="#FF0033"><B>*</B></FONT>
<select name="phase" 
<% if (mode!=null) {if (mode.equals(ftrUp.DELETE_CMD)) {%> style="width:80px; <%= mode_del%>" disabled <% }} %>
<% if (mode!=null) {if (!mode.equals(ftrUp.DELETE_CMD)) {%> style="width:80px" enabled <% }} %>
> <OPTION <%= phase1 %>/> . <OPTION <%= phase2 %>/> 0
</TD>
</TR>


<TR id="row2">
<TD ALIGN="RIGHT">SCORE</TD>
<TD><INPUT TYPE="text" NAME="score" VALUE="<%= score %>" SIZE=50 MAXLENGTH=30 style="width:350px;height:18px; <%= mode_del %>"></TD>
</TR>

<TR id="row1">
<TD ALIGN="RIGHT">LINK</TD>
<TD><INPUT TYPE="text" NAME="link" VALUE="<%= link %>" SIZE=50 MAXLENGTH=300 style="width:350px;height:18px; <%= mode_del %>"></TD>
</TR>

<TR id="row2">
<TD ALIGN="RIGHT">NOTE</TD>
<TD><textarea name="attributes" rows=10 cols=50 style="width:350px;height:200px; <%= mode_del %>;"><%=attributes%></textarea></TD>
</TR>

<TR id="row1">
<TD ALIGN="RIGHT">COMMIT MESSAGE</TD>
<TD><INPUT TYPE="text" NAME="commit_msg" VALUE="<%= commit_msg %>" SIZE=50 MAXLENGTH=300 style="width:350px;height:18px; "></TD>
</TR>

<TR id="row1">

<TD><ALIGN="RIGHT">GROUP</TD>
<TD colspan="4"><select name="groups" onchange="form1.ccc.value=this.selectedIndex"
<% if (mode!=null) { if (mode.equals(ftrUp.DELETE_CMD)) {%> style="width:300px; <%= mode_del%>" disabled <% }} %>
<% if (mode!=null) { if (!mode.equals(ftrUp.DELETE_CMD)) {%>  style="width:300px" enabled <% }} %>
> <OPTION> <%=groups %>
</select> 
<A href="add_group.jsp?action=add&uri=<%=system_uri%>"><%=group_link%></A>
</TD>
</TR>

<TR id="row2">
<TD></TD> 
<TD ALIGN="RIGHT"><P><INPUT TYPE="submit" class="red-button" NAME="submit"  
<% if (mode!=null) { if (mode.equals(ftrUp.CREATE_CMD)) {%> VALUE=" ADD " <%}}%>
<% if (mode!=null) { if (mode.equals(ftrUp.MODIFY_CMD)) {%> VALUE=" MODIFY " <%}}%>
<% if (mode!=null) { if (mode.equals(ftrUp.DELETE_CMD)) {%> VALUE=" DELETE " <%}}%>
SIZE =45></P></TD>
</TR>

</table>
</div>

</FORM>

<%if(!mode.equals(ftrUp.DELETE_CMD)) { %>
	<FORM NAME="form1" action="writebackDeleteGroup" method="GET">
	<div id="table">
	<table>
		<TR id="row0">
		<TD><INPUT TYPE="hidden" NAME="ccc" VALUE="" SIZE=50 MAXLENGTH=300 style="width:350px;height:18px; "></TD>
		</TR>
		<TR id="row2">
		<TD></TD> 
		<TD colspan="463" ALIGN="RIGHT" VALIGN="TOP"><P><INPUT TYPE="submit" class="red-button" NAME="submit"  VALUE="DELETE GROUP" 
		SIZE =45></P></TD>
		</TR>
        
	</table>
	</div>

	</FORM>
<%} %>

<p>
Back to   <A href="show_features.jsp?action=show&uri=<%=system_uri%>">feature list</A>. 
</p>

<jsp:include page="footer.jsp"/>